package com.cloudcc.mobile.service;

import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.IBinder;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.cloudcc.cloudframe.util.GsonUtil;
import com.cloudcc.cloudframe.util.LogUtils;
import com.cloudcc.mobile.AppContext;
import com.cloudcc.mobile.entity.DingWei;
import com.cloudcc.mobile.manager.LocationManager;
import com.cloudcc.mobile.manager.UserManager;
import com.cloudcc.mobile.net.KeepSessionClient;
import com.cloudcc.mobile.util.NetWork;
import com.cloudcc.mobile.util.Tools;

/**
 * 后台定时上传坐标位置service
 */
public class DingWeiService extends Service {
	private DingWei dw;
	private DWService service = new DWService();
	private Timer mTimer;
	private TimerTask mtask;

	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		LogUtils.d("app", "定位服务开启");
		super.onCreate();
		mTimer = new Timer();
		mtask =new LocTask();
		mTimer.schedule(mtask, 0, AppContext.locationtime);// 1000毫秒= 1秒;
	}
	
	private class LocTask extends TimerTask{
		@Override
		public void run() {
			if (NetWork.detect(DingWeiService.this)) {
				KeepSessionClient.getInstance().refreshServerBinding();
				LocationManager.getInstance().registerListener(mListener);
				LocationManager.getInstance().requestLocal();
			} else {
				LocationManager.getInstance().stopRequestLocal();
			}
		}
		
	}


	private void stopTask(){
		LocationManager.getInstance().stopRequestLocal();
		if (mTimer!=null) {
			mTimer.cancel();
			mTimer =null;
		}
		if (mtask!=null) {
			mtask.cancel();
			mTimer =null;
		}
	}

	@Override
	public void onDestroy() {
		LocationManager.getInstance().unregisterlistener(mListener);
		try {
			if (AppContext.isStopLocation) {
				stopTask();
				super.onDestroy();
			} else {
				Intent intent = new Intent(this, DingWeiService.class);
				startService(intent);
			}
		} catch (Exception e) {
			Tools.handle(e);
		}
	}

	private BDLocationListener mListener = new BDLocationListener() {
		@Override
		public void onReceiveLocation(BDLocation location) {
			if (null != location
					&& location.getLocType() != BDLocation.TypeServerError) {
				LocationManager.getInstance().stopRequestLocal();
				LocationManager.log(location);
				uploadeLoc(location);
				
			} else {
			}
		}
	};

	private void uploadeLoc(BDLocation location) {
		dw = new DingWei();
		dw.setYg(UserManager.getManager().getUser().userId);// 员工id
		String jingweidu =location.getLongitude() + "," +location.getLatitude();
		dw.setWz(jingweidu);// 经纬度
		dw.setMs(LocationManager.getDetailAddr(location, false));
		dw.setSj(Tools.getTime(System.currentTimeMillis()));
		new MyTask().execute(GsonUtil.Object2Json(jingweidu));
	}
	
	// 联网上传位置
	class MyTask extends AsyncTask<String, Void, String> {
		protected String doInBackground(String... params) {
			String jsonstring = "";
			try {
				String data = params[0];
				jsonstring = service.insert(data);
				return jsonstring;
			} catch (Exception e) {
				Tools.handle(e);
			}
			return jsonstring;
		}

		protected void onPostExecute(String result) {
			super.onPostExecute(result);
		}
	}
	
}
